#include <event2/event.h>
#include <event2/http.h>
#include <event2/buffer.h>
#include <event2/keyvalq_struct.h>
#include <iostream>
#include <string.h>

// HTTP请求处理回调函数
void http_request_handler(struct evhttp_request *req, void *arg) {
    struct evbuffer *response_buffer = evbuffer_new();
    struct evkeyvalq *headers;
    struct evkeyval *header;
    const char *uri = evhttp_request_get_uri(req);
    enum evhttp_cmd_type cmd_type = evhttp_request_get_command(req);
    const char *cmd_type_str = "";
    
    // 获取HTTP方法
    switch(cmd_type) {
        case EVHTTP_REQ_GET: cmd_type_str = "GET"; break;
        case EVHTTP_REQ_POST: cmd_type_str = "POST"; break;
        case EVHTTP_REQ_PUT: cmd_type_str = "PUT"; break;
        case EVHTTP_REQ_DELETE: cmd_type_str = "DELETE"; break;
        default: cmd_type_str = "OTHER";
    }
    
    // 解析URI
    struct evhttp_uri *decoded = evhttp_uri_parse(uri);
    const char *path = evhttp_uri_get_path(decoded);
    
    // 添加响应内容
    evbuffer_add_printf(response_buffer, 
        "<html><body>\n"
        "<h1>HTTP Request Details</h1>\n"
        "<p><b>Method:</b> %s</p>\n"
        "<p><b>URI:</b> %s</p>\n"
        "<p><b>Path:</b> %s</p>\n",
        cmd_type_str, uri, path ? path : "/");
    
    // 解析并显示请求头部
    headers = evhttp_request_get_input_headers(req);
    evbuffer_add_printf(response_buffer, "<h2>Request Headers:</h2>\n<ul>\n");
    for (header = headers->tqh_first; header; header = header->next.tqe_next) {
        evbuffer_add_printf(response_buffer, 
            "<li><b>%s:</b> %s</li>\n", 
            header->key, header->value);
    }
    evbuffer_add_printf(response_buffer, "</ul>\n");
    
    // 如果是POST请求，解析请求体
    if (cmd_type == EVHTTP_REQ_POST) {
        struct evbuffer *input_buffer = evhttp_request_get_input_buffer(req);
        size_t len = evbuffer_get_length(input_buffer);
        if (len > 0) {
            char *data = (char*)malloc(len + 1);
            evbuffer_copyout(input_buffer, data, len);
            data[len] = '\0';
            evbuffer_add_printf(response_buffer, 
                "<h2>Request Body:</h2>\n<pre>%s</pre>\n", data);
            free(data);
        }
    }
    
    evbuffer_add_printf(response_buffer, "</body></html>\n");
    
    // 设置响应头部
    evhttp_add_header(evhttp_request_get_output_headers(req), 
                      "Content-Type", "text/html");
    
    // 发送响应
    evhttp_send_reply(req, 200, "OK", response_buffer);
    
    // 清理资源
    evhttp_uri_free(decoded);
    evbuffer_free(response_buffer);
}

int main() {
    // 1. 创建事件基础框架event_base
    struct event_base *base = event_base_new();
    if (!base) {
        std::cerr << "Failed to create event base\n";
        return 1;
    }
    
    // 创建HTTP服务器
    struct evhttp *http_server = evhttp_new(base);
    if (!http_server) {
        std::cerr << "Failed to create HTTP server\n";
        return 1;
    }
    
    // 绑定到端口8080
    if (evhttp_bind_socket(http_server, "0.0.0.0", 8080) != 0) {
        std::cerr << "Failed to bind to port 8080\n";
        return 1;
    }
    
    // 设置通用请求处理回调
    evhttp_set_gencb(http_server, http_request_handler, NULL);
    
    std::cout << "HTTP server starting on port 8080...\n";
    
    // 启动事件循环
    event_base_dispatch(base);
    
    // 清理资源
    evhttp_free(http_server);
    event_base_free(base);
    
    return 0;
}